package cn.exrick.xboot.common.activeMQ;


import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.Result;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.jms.Destination;

/**
 * xbw
 * ActiveMQ,子系统推送信息接口
 */
@Slf4j
@RestController
@RequestMapping(value = "/portalService", produces = "application/json;charset=UTF-8")
public class ProducerRest {
    @Autowired
    ActiveManager activeManager;

    /**
     * post新闻公告接口
     */
    @RequestMapping(value = "/Message", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> addQueue(@RequestBody JSONObject message) {
        try {
            Destination destination = new ActiveMQQueue("portalnews");
            log.info("生产者："+message.toJSONString());
            activeManager.send(destination,message.toJSONString());//传入队列以及值
            return new ResultUtil<Object>().setSuccessMsg("新闻信息推送成功");
        } catch (JSONException e) {
            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("新闻信息推送失败，请重新推送");
        }
    }


    /**
     *post代办，预警接口
     */
    @RequestMapping(value = "/Messagechanging", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> addQueuelist(@RequestBody JSONObject numchange) {
        log.info("测试推动代办是否到达："+numchange.toJSONString());
        Destination destination = new ActiveMQQueue("change");
        try {
            activeManager.send(destination, numchange.toJSONString());
            return new ResultUtil<Object>().setSuccessMsg("待办已办信息推送成功");
        } catch (JSONException e) {
            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("待办已办推送失败，请重新推送");
        }
    }

    /**
     * add njp 20200514
     *post初始化待办已办的生产者
     */
    @RequestMapping(value = "/initBacklog", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> initBacklog(@RequestBody JSONObject data) {
        log.info("初始化数据报文："+data.toJSONString());
        Destination destination = new ActiveMQQueue("initBacklog");
        try {
            activeManager.send(destination, data.toJSONString());
            return new ResultUtil<Object>().setSuccessMsg("初始待办数据推送成功");
        } catch (JSONException e) {
            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("初始待办数据推送失败，请重新推送");
        }
    }

    /**
     * add njp 20200527
     *post检测三方系统连接状态逻辑的生产者
     */
    @RequestMapping(value = "/checkConn", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> checkConn(@RequestBody JSONObject data) {
        log.info("检测三方系统连接状态报文："+data.toJSONString());
        Destination destination = new ActiveMQQueue("checkConn");
        try {
            activeManager.send(destination, data.toJSONString());
            return new ResultUtil<Object>().setSuccessMsg("检测三方系统连接状态报文推送成功");
        } catch (JSONException e) {
            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("检测三方系统连接状态报文推送失败，请重新推送");
        }
    }

    /**
     * 2019/10/10
     * 在线教育推送接口
     */
    @RequestMapping(value = "/UnfinishedCourse", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> courseOnline(@RequestBody JSONObject unfinished) {
        Destination destination = new ActiveMQQueue("course");
        log.info("测试在线教育的推动否到达：" + unfinished.toJSONString());
        try {
            activeManager.send(destination, unfinished.toJSONString());
            return new ResultUtil<Object>().setSuccessMsg("教育课程信息信息推送成功");
        } catch (JSONException e) {

            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("教育课程信息推送失败，请重新推送");
        }
    }
    /**
     * 2019/10/28
     * 预留预警推送接口，共享系统保持查询，为其他第三方准备
     */
    @RequestMapping(value = "/WarningInfo", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> warningInfo(@RequestBody JSONObject warningInfo) {
        Destination destination = new ActiveMQQueue("warningInfo");
        log.info("测试预警接口的推动否到达：" + warningInfo.toJSONString());
        try {
            activeManager.send(destination, warningInfo.toJSONString());
            return new ResultUtil<Object>().setSuccessMsg("预警信息信息推送成功");
        } catch (JSONException e) {
            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("预警信息推送失败，请重新推送");
        }
    }

    /**
     * add njp 20200514
     *post初始化待办已办的生产者
     */
    /*@RequestMapping(value = "/topicTest", method = RequestMethod.POST,consumes="application/json;charset=utf-8")
    @ResponseBody
    public Result<Object> topicTest(@RequestBody JSONObject data) {
        log.info("topicTest");
        Destination destination = new ActiveMQTopic("topicTest");
        try {
            activeManager.send(destination, data.toJSONString());
            return new ResultUtil<Object>().setSuccessMsg("topicTest数据推送成功");
        } catch (JSONException e) {
            e.printStackTrace();
            return new ResultUtil<Object>().setErrorMsg("topicTest数据推送失败，请重新推送");
        }
    }*/

}
